package org.ezplatform.office.questionnaire.service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.commons.collections.CollectionUtils;
import org.ezplatform.core.dao.jpa.JpaBaseDao;
import org.ezplatform.core.service.BaseService;
import org.ezplatform.office.questionnaire.dao.QuestionnaireTopicOptionsDao;
import org.ezplatform.office.questionnaire.entity.QuestionnaireTopicOptions;
import org.ezplatform.util.JsonUtils;
import org.ezplatform.util.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.fastjson.JSONObject;

/**
 * <br> 问卷题目选项接口类
 * <br> 创建时间 2017-11-23
 * @author wujs
 * @version 1.0
 * 
*/

@Service("questionnaireTopicOptionsService")
@Transactional
public class QuestionnaireTopicOptionsService extends BaseService<QuestionnaireTopicOptions,String>{
	
	private final static Logger LOGGER = LoggerFactory.getLogger(QuestionnaireTopicOptionsService.class);
	
	@Autowired
	@Qualifier("questionnaireTopicOptionsDao")
	private QuestionnaireTopicOptionsDao questionnaireTopicOptionsDao;
	
	@Override
	protected JpaBaseDao<QuestionnaireTopicOptions, String> getEntityDao() {
		return questionnaireTopicOptionsDao;
	}
	
	/**
	 * 获取要保存的问卷题目选项
	 * @param optionSetting 题目选项字符串
	 * @param topicId 题目id
	 * @return 问卷题目选项实体集合
	 */
	public List<QuestionnaireTopicOptions> getSaveOptionsInfo(String optionSetting,String topicId){
		List<QuestionnaireTopicOptions> list = new ArrayList<QuestionnaireTopicOptions>();
		List<String> listOption =Arrays.asList(optionSetting.split("option-option")) ;
		for(int i=0;i<listOption.size();i++){
			QuestionnaireTopicOptions op = new QuestionnaireTopicOptions();
			if(!"".equals(listOption.get(i))) {
				Map<String,Object> map = JSONObject.parseObject(listOption.get(i));
				String name= StringUtils.null2String(map.get("option"));
				String optionNo = StringUtils.null2String(map.get("optionOrder"));
//				Map<String,Object> mapOptionSet = (Map<String, Object>) map.get("optionSetting");
//				if(mapOptionSet !=null){
//					String optionSet =JsonUtils.writeValueAsString(mapOptionSet);
//					op.setOptionSetting(optionSet);
//				}
				op.setOptionSetting(StringUtils.null2String(map.get("optionSetting")));
				op.setName(name);
				if(StringUtils.isNotBlank(optionNo)){
					op.setOptionOrder(Integer.parseInt(optionNo));
				}
			}			
			op.setTopicId(topicId);
			list.add(op);
		}
		return list;
	}
	
	/**
	 * 获取要保存的问卷题目选项
	 * @param optionSetting 题目选项字符串
	 * @param childOptionSetting 题目选项子选项字符串
	 * @param topicId 题目id
	 * @return 问卷题目选项实体集合
	 */
	public List<QuestionnaireTopicOptions> getSaveOptionsInfo(String optionSetting,String childOptionSetting,String topicId){
		List<QuestionnaireTopicOptions> list = new ArrayList<QuestionnaireTopicOptions>();
		List<String> listOption =Arrays.asList(optionSetting.split("option-option")) ;
		for(int i=0;i<listOption.size();i++){
			Map<String,Object> map = JsonUtils.readValue(listOption.get(i));
			String name= (String) map.get("option");
			String optionNo=(String) map.get("optionOrder");
			QuestionnaireTopicOptions op = new QuestionnaireTopicOptions();
			op.setOptionSetting(childOptionSetting);
			op.setName(name);
			if(optionNo !=null){
				op.setOptionOrder(Integer.parseInt(optionNo));
			}
			op.setTopicId(topicId);
			list.add(op);
		}
		return list;
	}
	
	/**
	 * 删除题目选项
	 * @param list 题目ID
	 * @return 删除成功返回true
	 */
	public boolean deleteOptionsByTopicId(List<String> list){
		questionnaireTopicOptionsDao.deleteOptionsByTopicId(list);
		return true;
	}
	
	/**
	 * 获取题目选项信息
	 * @param topicId 题目ID
	 * @return 题目选项实体集合
	 */
	public List<QuestionnaireTopicOptions> getOptionsByTopicId(String topicId){
		return questionnaireTopicOptionsDao.getOptionsByTopicId(topicId);
	}
	
	
	
	/*===========================================钉钉方法==========================================*/
	/**
	 * 获取要保存的问卷题目选项
	 * @param optionSetting 题目选项字符串
	 * @param topicId 题目id
	 * @return 问卷题目选项实体集合
	 */
	public List<QuestionnaireTopicOptions> getDingSaveOptionsInfo(List optionSetting,String topicId,String userId,String orgId){
		List<QuestionnaireTopicOptions> list = new ArrayList<QuestionnaireTopicOptions>();
		//List<String> listOption =Arrays.asList(optionSetting.split("option-option")) ;
		for(int i=0;i<optionSetting.size();i++){
			Map<String,Object> map = (Map<String, Object>) optionSetting.get(i);
			String name= (String) map.get("optionsContent");
			int optionNo = i+1;
			Map<String,Object> mapOptionSet = (Map<String, Object>) map.get("optionSetting");
			QuestionnaireTopicOptions op = new QuestionnaireTopicOptions();
			if(mapOptionSet !=null){
				if(mapOptionSet.get("optionImg")!= null){
					String imgIds = "";
					List<Map<String,Object>> optionImgList = (List<Map<String, Object>>) mapOptionSet.get("optionImg");
					if(optionImgList.size()>0) {
						for(Map<String,Object> item:optionImgList) {
							imgIds += item.get("id").toString();
						}
					}
					mapOptionSet.put("optionImg", imgIds);
				}
				String optionSet =JsonUtils.writeValueAsString(mapOptionSet);
				op.setOptionSetting(optionSet);
			}
			op.setCreatedBy(userId);
			op.setCreatedOrg(orgId);
			op.setCreatedDate(new Date());
			op.setName(name);
			op.setOptionOrder(optionNo);
			op.setTopicId(topicId);
			list.add(op);
		}
		return list;
	}
	
}
